//
//  HERNAuthViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/7.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HERNAuthViewController.h"
#import "HEBindInputCell.h"

@interface HERNAuthViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *formArrs;

@end

@implementation HERNAuthViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = KWhiteColor;
    
    [self zx_setLeftBtnWithImg:IMAGE_NAMED(@"back_black") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    self.title = @"实名认证";
    [self.view addSubview:self.tableView];
  
    [self initFooterView];
    [self initCell];
}

#pragma mark -
- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];

    NSMutableArray *classArr = [NSMutableArray array];
 

    //  姓名
    GDFiterCellModel *nameModel = [GDFiterCellModel new];
    nameModel.cellKlass = HEBindInputCell.class;
    nameModel.leftTitle = @"姓名";
    nameModel.tableKey = @"bd_name";
    nameModel.placeHolder = @"请输入姓名";

    //  身份证
    GDFiterCellModel *idnoModel = [GDFiterCellModel new];
    idnoModel.cellKlass = HEBindInputCell.class;
    idnoModel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    idnoModel.leftTitle = @"身份证号";
    idnoModel.tableKey = @"bd_idNo";
    idnoModel.placeHolder = @"请输入身份证号";
 

    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[nameModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];
    
    _sections = @[nameModel, idnoModel].mutableCopy;

    [_tableView reloadData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStyleGrouped];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)initFooterView {
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 300)];
    
    // 保存按钮
    QMUIFillButton *saveBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    saveBtn.titleLabel.font = DYFont(16);
    [saveBtn setBackgroundColor:CNavBgColor];
    [saveBtn setTitle:@"立即认证" forState:UIControlStateNormal];
    [footerView addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerView).mas_offset(30);
        make.left.equalTo(footerView).mas_offset(50);
        make.right.equalTo(footerView).mas_offset(-50);
        make.height.mas_offset(40);
    }];
  
    self.tableView.tableFooterView = footerView;
}


- (void)saveClick {
  
    if ([DYTools isBlankString: self.formModel.bd_name]) {
        [Dialog toastBottom:@"请输入姓名"];
        return;
    }
    
    if (![WOValidate isValidateIdentity: self.formModel.bd_idNo]) {
         [Dialog toast:@"身份证号格式错误" delay:2];
        return;
    }
    
    NSMutableDictionary *params = @{}.mutableCopy;
    [params setValue:self.formModel.bd_name forKey:@"name"];
    [params setValue:self.formModel.bd_idNo forKey:@"card_no"];

    [WOAFNetWorkingHandle POSTWithBaseUrlByKeyValue:KServerUrl suffixUrl:KrealnameRecord parameters: params success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [Dialog toastBottom:@"实名认证成功"];
            UserInfo *data = [UserInfo mj_objectWithKeyValues:response.data];
            // 存储用户数据
            [[UserManager sharedUserManager] saveUserInfo:data];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationRealAuth object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationUserInfoChange object:nil];
            if (self.backBlock) {
                self.backBlock();
            }
            [self dismissViewControllerAnimated:YES completion:^{
            }];
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
 
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sections.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
    
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
 
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
               [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
    }];
}

#pragma mark -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 15)];
    v.backgroundColor = CViewBgColor;
    return v;
}

#pragma mark 解决cell分割线左侧留空的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 下面这几行代码是用来设置cell的上下行线的位置
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    //按照作者最后的意思还要加上下面这一段，才能做到底部线控制位置，所以这里按stackflow上的做法添加上吧。
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}


@end
